package file

import (
	"bufio"
	"fmt"
	"io"
	"log"
	"os"
	"tyy_linux_system/utils"
)

// 全局变量，记录secure文件行数
var lineCount int

// 测试变量，记录打印了多少次的变量
var printCount int

// 临时函数变量
var FileTmpFunc = func() {
	//fmt.Print("--------")
	linesRet := ReadEntireFile("test.txt")
	//fmt.Print(len(linesRet))
	//fmt.Println("---------")
	PrintLatestLines(linesRet, LineDiffNum(len(linesRet)))
	UpdateLineCount(len(linesRet))
}
var FileTmpFunc2 = func() {
	fmt.Print("--------")
	linesRet := ReadEntireFile("test.txt")
	fmt.Print(len(linesRet))
	fmt.Println("---------")
	fmt.Println(ReturnLatestLines(linesRet, LineDiffNum(len(linesRet))))
	UpdateLineCount(len(linesRet))
}

//	var FileTmpFunc3 = func() {
//		fmt.Print("--------")
//		linesRet := ReadEntireFile("test.txt")
//		fmt.Print(len(linesRet))
//		fmt.Println("---------")
//		latestLinesRet := ReturnLatestLines(linesRet, LineDiffNum(len(linesRet)))
//		fmt.Println(matchDateTime(latestLinesRet))
//		UpdateLineCount(len(linesRet))
//	}
var FileTmpFunc4 = func() {
	linesRet := ReadEntireFile("test.txt")
	fmt.Println("--------", len(linesRet), "---------")
	latestLinesRet := ReturnLatestLines(linesRet, LineDiffNum(len(linesRet)))
	fmt.Println(matchRefuseConnect(latestLinesRet))
	if len(matchRefuseConnect(latestLinesRet)) != 0 {
		fmt.Println(matchRefuseConnect(latestLinesRet)[0])
	}
	UpdateLineCount(len(linesRet))
}
var FileTmpFunc5 = func() {
	linesRet := ReadEntireFile("test.txt")
	fmt.Println("--------", len(linesRet), "---------")
	latestLinesRet := ReturnLatestLines(linesRet, LineDiffNum(len(linesRet)))
	if ok, strRet := isMatchRefuseConnect(latestLinesRet); ok {
		refuseConnectModelRet := matchRefuseConnect(strRet)
		if len(refuseConnectModelRet) != 0 {
			fmt.Println(refuseConnectModelRet[0])
			if len(refuseConnectModelRet) > 1 {
				fmt.Println(refuseConnectModelRet[1])
			}
		}
		for _, m := range refuseConnectModelRet {
			err := m.Save("./test.db")
			if err != nil {
				log.Println(err)
			}
		}
	}
	UpdateLineCount(len(linesRet))
}

// 读取整个文件到内存中，遍历每一行并放入切片中
func ReadEntireFile(filepath string) []string {
	file, err := os.Open(filepath)
	if err != nil {
		fmt.Println("Error opening file:", err)
		return nil
	}
	defer file.Close()
	reader := bufio.NewReader(file)
	var lines []string
	for {
		line, err := reader.ReadString('\n')
		if err != nil {
			if err == io.EOF {
				break
			}
			fmt.Println(lines, line)
		}
		lines = append(lines, line)
	}
	return lines
}

// 打印文件最新变动的行
func PrintLatestLines(linesRet []string, linesDiffNum int) {
	for i := len(linesRet) - 1; i >= 0 && len(linesRet)-i <= linesDiffNum; i-- {
		if printCount == 0 {
			break
		}
		fmt.Print(linesRet[i])
	}
	printCount++
}

// 返回文件最新变动的行的切片
func ReturnLatestLines(linesRet []string, linesDiffNum int) []string {
	latestLinesRet := make([]string, 0)
	for i := len(linesRet) - 1; i >= 0 && len(linesRet)-i <= linesDiffNum; i-- {
		if printCount == 0 {
			printCount++
			return nil
		}
		latestLinesRet = append(latestLinesRet, linesRet[i])
	}
	return utils.ReverseStringRet(latestLinesRet)
}

// 更新全局变量（文件行数），
func UpdateLineCount(newLineCount int) {
	lineCount = newLineCount
}

// 计算文件变动了多少行
func LineDiffNum(newNum int) int {
	fmt.Sprintf("new line count is %d, old line count is %d", newNum, lineCount)
	return newNum - lineCount
}
